package cn.glutnn.service.impl;

import cn.glutnn.entity.Result;
import cn.glutnn.mapper.ExpertMapper;
import cn.glutnn.service.ExpertService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.Expert;

import java.util.List;
import java.util.Map;

/**
 * (Expert)表服务实现类
 *
 * @author msh
 * @since 2024-10-25 09:33:27
 */
@Service("expertService")
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert> implements ExpertService {

    @Override
    public Result<Expert> selectExpert() {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        Expert ex = getOne(new QueryWrapper<Expert>().eq("user_name", o1));


        return Result.success(ex);
    }

    @Override
    public Result<List<Expert>> selectAllExpert() {
        List<Expert> list = list();
        return Result.success(list);
    }

    @Override
    public Result deleteExpertByName(String userName) {
        boolean remove = remove(new QueryWrapper<Expert>().eq("user_name", userName));

        return Result.success();
    }

    @Override
    public Result updateExpert(Expert expert) {
        boolean update = update(expert, new QueryWrapper<Expert>().eq("user_name", expert.getUserName()));

        return Result.success();
    }

    @Override
    public Result addExpert(Expert expert) {
        boolean save = save(expert);
        return Result.success();
    }

    @Override
    public Result addUpdexpert(Expert expert) {
        String userName = expert.getUserName();
        if (getOne(new QueryWrapper<Expert>().eq("user_name", userName)) == null) {
            return addExpert(expert);
        } else {
            return updateExpert(expert);
        }

    }
}

